package servlet;

import hibernate.RequisitionInfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import hibernate.Role;
import hibernate.User;
import DAO.UserDAO;

/**
 * Servlet implementation class SearchYH
 */
public class SearchYH extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchYH() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		
		String start = request.getParameter("start");
		String limit = request.getParameter("limit");
		String userName = request.getParameter("UserName");
		String trueName = request.getParameter("trueName");

		UserDAO u = new UserDAO();
		Vector v = new Vector();
	    
	    if(("".equals(userName))&&("".equals(trueName)))
	    {
	    	v = u.getAll(start,limit);   	
	    }
	    else
	    {
	    	v = u.searchUser(start,limit,userName,trueName);
	    }
	    
	    String count = (v.elementAt(0)).toString();
	    @SuppressWarnings("unchecked")
	    List list = (List)v.elementAt(1);
	    
		String s = "{totalCount:" + count + "," + "result:[";
		int SizeOfList=list.size();
		User user;
		String RoleOfUser;
		for(int i=0;i<SizeOfList;i++)
		{   
			user = ((User)list.get(i));
			Set<Role> sr = user.getRoles();
		    Iterator sit = sr.iterator();
		    RoleOfUser="";
		    for(;sit.hasNext();)
		    {
		    	RoleOfUser += ((Role)sit.next()).getName()+" ";
		    }
			s += "{UserID:" +"'"+  user.getUserID() + "',";
			s += "UserName:" + "'" + user.getUserName() + "',";
			s += "Password:" + "'" + user.getPassword() + "',";
			s += "trueName:" + "'" + user.getTrueName() + "',";
			s += "Phone:" + "'" + user.getPhone() + "',";
			s += "Unit:" + "'" + user.getUnit() + "',";
			s += "Job:" + "'" + user.getJob() + "',";
			s += "Email:" + "'" + user.getEmail() + "',";
			s += "workPhone:" + "'" + user.getWorkPhone() + "',";
			s += "mobilePhone:" + "'" + user.getMobilePhone() + "',";
			s += "BelongtoRole:" + "'" +RoleOfUser+ "'}";	
			if(i!=SizeOfList-1)
				s += ",";
		}
        s = s + "]}";
        out.write(s);
        System.out.println(s);
        out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
